var basePath = $("#basePath").val();
$(document).ready(function (){
    querySearchList();
});

function querySearchList(){
        var keyword = $("#keyword").val();
        var url = basePath+"/device/queryDeviceByKeyword";
        publicAjax(url,"POST",{keyword:keyword},function (data){
                if(data.ret == 200){
                    bindDeviceDataByKeyword(data.data);
                }else{
                    flavrShowByTime(data.msg,null,"danger",false);
                }
        });
}


function bindDeviceDataByKeyword(obj){

        $(".bg-green").html(obj.device.length);
        $(".bg-red").html(obj.repair.length);
        $(".bg-blue").html(obj.contract.length);


        $("#device_tbody").html("");
        $("#repair-tbody").html("");
        if(obj.device == null || obj.device.length == 0){
            $("#device_tbody").html("<tr><td class='text-left text-muted' colspan='8'>暂无数据</td></tr>");
        }else{
            var html = "";
            $.each(obj.device,function (index,item) {
                html+= "<tr><td class='text-left text-muted'>"+(index+1)+"</td>" +
                    "<td class='text-left text-muted'><a href='javascript:deviceInfo("+item.deviceId+")'>"+(item.deviceName==null ? '' : item.deviceName) + "</a></td>" +
                    "<td class='text-left text-muted'>"+(item.deviceModel==null ? '' : item.deviceModel)+"</td>" +
                    "<td class='text-left text-muted'>"+(item.sn==null ? '' : item.sn)+"</td>" +
                    "<td class='text-left text-muted'>"+(item.hospitalName==null ? '' : item.hospitalName)+"</td>" +
                    "<td class='text-left text-muted'>"+(item.department==null ? '' : item.department)+"</td>" +
                    "<td class='text-left text-muted'>"+(item.manufacturerName==null ? '' : item.manufacturerName)+"</td>" +
                    "<td class='text-left text-muted'>"+(item.deviceStatusItem==null ? '' : item.deviceStatusItem)+"</td>" +
                    "</tr>";
            });
            $("#device_tbody").html(html);
        }
        if(obj.repair == null || obj.repair.length == 0){
            $("#repair-tbody").html("<tr><td class='text-center text-muted' colspan='8'>暂无数据</td></tr>");
        }else{
            var html = "";
            $.each(obj.repair,function (index,item) {
                html+= "<tr><td class='text-left text-muted'>"+(index+1)+"</td>" +
                    "<td class='text-left text-muted'> <a href='javascript:repairInfo("+item.repairId+")'> "+(item.repairNo==null ? '' : item.repairNo)+"</a></td>" +
                    "<td class='text-left text-muted'>"+(item.deviceName==null ? '' : item.deviceName)+"</td>" +
                    "<td class='text-left text-muted'>"+(item.deviceModel==null ? '' : item.deviceModel)+"</td>" +
                    "<td class='text-left text-muted'>"+(item.sn==null ? '' : item.sn)+"</td>" +
                    "<td class='text-left text-muted'>"+(item.hospitalName==null ? '' : item.hospitalName)+"</td>" +
                    "<td class='text-left text-muted'>"+(item.manufacturerName==null ? '' : item.manufacturerName)+"</td>" +
                    "<td class='text-left text-muted'>"+(item.deviceStatusItem==null ? '' : item.deviceStatusItem)+"</td>" +
                    "</tr>";
            });
            $("#repair-tbody").html(html);
        }

        if(obj.contract == null || obj.contract.length == 0){
            $("#contract-tbody").html("<tr><td class='text-center text-muted' colspan='8'>暂无数据</td></tr>");
        }else{
            var html = "";
            $.each(obj.contract,function (index,item) {
                html+= "<tr><td class='text-left text-muted'>"+(index+1)+"</td>" +
                    "<td class='text-left text-muted'> <a href='javascript:contractInfo("+item.contractId+")'> "+(item.contractNo==null ? '' : item.contractNo)+"</a></td>" +
                    "<td class='text-left text-muted'>"+(item.deviceName==null ? '' : item.deviceName)+"</td>" +
                    "<td class='text-left text-muted'>"+(item.deviceModel==null ? '' : item.deviceModel)+"</td>" +
                    "<td class='text-left text-muted'>"+(item.hospitalName==null ? '' : item.hospitalName)+"</td>" +
                    "<td class='text-left text-muted'>"+(item.manufacturerName==null ? '' : item.manufacturerName)+"</td>" +
                    "</tr>";
            });
            $("#contract-tbody").html(html);
        }

}


function deviceInfo(id) {
    location.href = basePath +"/device/info?id="+id;
}

function repairInfo(id){
    location.href=basePath+"/repair/info?id="+id
}

function contractInfo(id){
    location.href = basePath+"/contract/info?id="+id;
}